package phaseII;

import java.util.ArrayList;

/**
 * Gets variables from VarTable and passes them as parameters to Executioner
 */

@SuppressWarnings("unchecked")
public class FunctionCaller {
	
	private VarTable varTable;
	private Executioner executioner;
	
	public FunctionCaller(VarTable varTable) {
		this.varTable = varTable;
		executioner = new Executioner();
	}
	
	public VarValue findMatches(String regex, String filePath) {
		RegexValidator.validate(regex);
		ArrayList<String> matches = executioner.find(regex, filePath);
		if (matches == null)
			return null;
		else {
			VarValue varVal = new VarValue(matches, ArrayList.class);
			return varVal;
		}
	}
	
	public void replace(String regex, String replacement, String srcFile, String dstFile) {
		RegexValidator.validate(regex);
		// TODO Auto-generated method stub
		//executioner.replace(regex, replacement, srcFile, dstFile);
	}

	public void recursiveReplace(String regex, String replacement, String srcFile, String dstFile) {
		RegexValidator.validate(regex);
		// TODO Auto-generated method stub
		//executioner.recursiveReplace(regex, replacement, srcFile, dstFile);
	}

	public String getMaxFreqString(String varID) {
		VarValue var = varTable.getVar(varID);
		if (var == null || !(var.getData() instanceof ArrayList<?>))
			return "Can't find frequent string because list is empty.";
		else {
			ArrayList<String> data = (ArrayList<String>) var.getData();
			String mostFreq = "Frequent";//executioner.getMaxFreqString(data);
			if (mostFreq == null)
				mostFreq = "Can't find frequent string because list is empty.";
			return mostFreq;
		}
	}
	
	public Integer matchedListLength(String varName) {
		VarValue var = varTable.getVar(varName);
		if (var != null && var.getData().getClass() == ArrayList.class) {
			ArrayList<String> matchedList = (ArrayList<String>) var.getData();
			if (matchedList == null)
				return null;
			else
				return matchedList.size();
		}
		else
			return null;
	}

	public String print(VarValue exp) {
		//exp holds number
		if (exp == null)
			return null;
		if (exp.getData() instanceof Integer) 
			return exp.getClass().toString();
		//exp holds ID of variable to print
		else if (exp.getData() instanceof String) {
			String varName = (String) exp.getData();
			VarValue var = varTable.getVar(varName);
			if (var == null)
				return null;
			else
				return var.getData().toString();
		}
		//exp holds arraylist
		else {
			ArrayList<String> matches = (ArrayList<String>) exp.getData();
			return matches.toString();
		}
	}

	public ArrayList<String> union(ArrayList<String> leftData, ArrayList<String> rightData) {
		ArrayList<String> result = executioner.union(leftData, rightData);
		return result;
	}

	public ArrayList<String> difference(ArrayList<String> leftData, ArrayList<String> rightData) {
		ArrayList<String> result = executioner.diff(leftData, rightData);
		return result;
	}

	public ArrayList<String> intersection(ArrayList<String> leftData, ArrayList<String> rightData) {
		ArrayList<String> result = executioner.inters(leftData, rightData);
		return result;
	}
	
}
